package _18_剑指OfferII;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

// 可以使用HashMap记录长度相同的数组一组，然后比较长度相同的数据，是否与其差值为1
// HashMap存储长度类似剪枝，直接达到快速查询的效果
public class _064_剑指OfferII神奇的字典 {

    Set<String> containers;

    /**
     * Initialize your data structure here.
     */
    public _064_剑指OfferII神奇的字典() {
        containers = new HashSet<>();
    }

    public void buildDict(String[] dictionary) {
        containers.addAll(Arrays.asList(dictionary));
    }

    public boolean search(String searchWord) {
        char[] chars = searchWord.toCharArray();
        for (int i = 0; i < chars.length; ++i) {
            char curChar = chars[i];
            for (int j = 'a'; j <= 'z'; ++j) {
                if (j == curChar) continue;
                chars[i] = (char) j;
                if (containers.contains(new String(chars))) return true;
                chars[i] = curChar;
            }
        }
        return false;
    }

}
